package com.qlyl.modules.information.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qlyl.common.constant.CommonCheckStatus;
import com.qlyl.common.util.oConvertUtils;
import com.qlyl.modules.information.entity.AppUser;
import com.qlyl.modules.information.entity.AuthInfo;
import com.qlyl.modules.information.entity.Consumers;
import com.qlyl.modules.information.mapper.AuthInfoMapper;
import com.qlyl.modules.information.service.IAppUserService;
import com.qlyl.modules.information.service.IAuthInfoService;
import com.qlyl.modules.information.service.IConsumersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @Description: 认证审核信息
 * @Author: jeecg-boot
 * @Date:   2020-01-09
 * @Version: V1.0
 */
@Service
public class AuthInfoServiceImpl extends ServiceImpl<AuthInfoMapper, AuthInfo> implements IAuthInfoService {

    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IConsumersService consumersService;

    @Override
    @Transactional
    public boolean save(AuthInfo entity) {

        //修改用户和企业认证状态:认证中（只改状态，不邦定值）
        this.appUserService.update(new AppUser().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_CHECKing)),
                new UpdateWrapper<AppUser>().lambda().eq(AppUser::getId, entity.getUserid()));
        this.consumersService.update(new Consumers().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_CHECKing)),
                new UpdateWrapper<Consumers>().lambda().eq(Consumers::getId, entity.getComsumsId()));

        return super.save(entity);
    }


    @Override
    @Transactional
    public void frozenBatch(String id, String status) {
            AuthInfo authInfo = getById(id);
            String userid = authInfo.getUserid();
            String comsumsId = authInfo.getComsumsId();

            //修改审核表状态
            this.update(new AuthInfo().setAuthCheckStatus(Integer.valueOf(status)).setCheckTime(new Date()),
                    new UpdateWrapper<AuthInfo>().lambda().eq(AuthInfo::getId, id));

            //根据认证结果修改用户和企业认证状态
            if(status.equals(CommonCheckStatus.Auth_CHECK_SUCCESS)){//认证成功：修改用户和企业状态
                this.appUserService.update(new AppUser().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_CHECKED)).setConsumersId(comsumsId),
                        new UpdateWrapper<AppUser>().lambda().eq(AppUser::getId, userid));
                this.consumersService.update(new Consumers().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_CHECKED)).setBindingUserid(userid),
                        new UpdateWrapper<Consumers>().lambda().eq(Consumers::getId, comsumsId));

            }else if(status.equals(CommonCheckStatus.Auth_CHECK_FAIL)){//认证失败：清除用户企业状态
                this.appUserService.update(new AppUser().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_UNCHECK)),
                        new UpdateWrapper<AppUser>().lambda().eq(AppUser::getId, userid));
                this.consumersService.update(new Consumers().setBindingStatus(Integer.valueOf(CommonCheckStatus.Auth_UNCHECK)),
                        new UpdateWrapper<Consumers>().lambda().eq(Consumers::getId, comsumsId));
            }


    }
}
